<template>
  <div ref="login" :class="prefixCls" class="relative w-full h-full px-4 h-100vh">
    <!-- 右上角功能按钮 -->
    <AppLocalePicker class="absolute text-white top-4 right-4 enter-x xl:text-gray-600 z-10" :showText="false" v-if="!sessionTimeout && showLocale" />
    <AppDarkModeToggle class="absolute top-3 right-7 enter-x z-10" v-if="!sessionTimeout" @change="handleThemeChange" />

    <!-- 移动端Logo -->
    <span class="-enter-x xl:hidden z-10">
      <AppLogo :alwaysShowTitle="true" />
    </span>

    <!-- 主要内容区域 -->
    <div class="login-content">
      <!-- 左侧表单区域 -->
      <div class="left-section">
        <div :class="`${prefixCls}-form`" class="login-form-wrapper">
          <ModernLoginForm />
          <ModernRegisterForm />
          <ModernForgetPasswordForm />
          <MobileForm />
          <QrCodeForm />
        </div>
      </div>
    </div>

    <!-- 页脚 -->
    <footer class="absolute bottom-0 left-0 right-0 py-4 text-center text-sm text-white/80 dark:text-gray-400 z-10">
      <div class="container mx-auto">
        <p class="footer-text">
          Copyright © {{ year }} Echo Reminis. All Rights Reserved.
          <a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener noreferrer" class="footer-link"> 鲁ICP备2021039405号-1 </a>
        </p>
      </div>
    </footer>
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, onUnmounted, ref, defineAsyncComponent } from 'vue';
  import { AppLogo } from '/@/components/Application';
  import { AppLocalePicker, AppDarkModeToggle } from '/@/components/Application';
  const ModernLoginForm = defineAsyncComponent(() => import('./ModernLoginForm.vue'));
  const ModernRegisterForm = defineAsyncComponent(() => import('./ModernRegisterForm.vue'));
  const ModernForgetPasswordForm = defineAsyncComponent(() => import('./ModernForgetPasswordForm.vue'));
  const MobileForm = defineAsyncComponent(() => import('./MobileForm.vue'));
  const QrCodeForm = defineAsyncComponent(() => import('./QrCodeForm.vue'));
  import { useDesign } from '/@/hooks/web/useDesign';
  import { useLocaleStore } from '/@/store/modules/locale';
  import { useLoginState } from './useLogin';
  import * as THREE from 'three';
  import VANTA from 'vanta/dist/vanta.rings.min';
  defineProps({
    sessionTimeout: {
      type: Boolean,
    },
  });
  let vantaEffect;
  const login = ref();

  const handleThemeChange = () => {
    // 延迟执行以确保主题已切换
    setTimeout(initVanta, 100);
  };

  const initVanta = () => {
    if (vantaEffect) {
      vantaEffect.destroy();
    }

    const bgColor = document.documentElement.getAttribute('data-theme') === 'dark' ? 0x151515 : 0xffffff;

    vantaEffect = VANTA({
      el: login.value,
      THREE: THREE,
      mouseControls: true,
      touchControls: true,
      gyroControls: false,
      minHeight: 200.0,
      minWidth: 200.0,
      scale: 1.0,
      scaleMobile: 1.0,
      backgroundColor: bgColor,
      color: 0x0,
    });
  };

  onMounted(() => {
    initVanta();
  });

  onUnmounted(() => {
    if (vantaEffect) {
      vantaEffect.destroy();
    }
  });
  const { prefixCls } = useDesign('login');
  const localeStore = useLocaleStore();
  const showLocale = localeStore.getShowPicker;
  const { handleBackLogin } = useLoginState();
  const year = new Date().getFullYear();
  handleBackLogin();
</script>
<style lang="less">
  @prefix-cls: ~'@{namespace}-login';
  @logo-prefix-cls: ~'@{namespace}-app-logo';
  @countdown-prefix-cls: ~'@{namespace}-countdown-input';
  @dark-bg: #293146;

  body {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }

  // 深色模式适配
  html[data-theme='dark'] {
    .@{prefix-cls} {
      background-color: @dark-bg;

      .ant-input,
      .ant-input-password {
        background-color: #232a3b;
      }

      .ant-btn:not(.ant-btn-link):not(.ant-btn-primary) {
        border: 1px solid #4a5569;
      }

      &-form {
        background: transparent !important;
      }

      .app-iconify {
        color: #fff;
      }



      .footer-text {
        color: rgba(255, 255, 255, 0.7) !important;
      }
    }

    input.fix-auto-fill,
    .fix-auto-fill input {
      -webkit-text-fill-color: #c9d1d9 !important;
      box-shadow: inherit !important;
    }
  }

  .@{prefix-cls} {
    min-height: 100vh;
    overflow: hidden;
    position: relative;
    display: flex;
    flex-direction: column;

    // 主要内容区域
    .login-content {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      padding: 0;
      position: relative;
      z-index: 1;
      width: 100%;

      @media (max-width: @screen-xl) {
        flex-direction: column;
        justify-content: flex-start;
        padding: 20px;
        padding-top: 80px;
      }
    }

    // 表单区域
    .left-section {
      flex: 0 0 auto;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      width: 50%;
      max-width: 600px;
      padding-left: 60px;

      @media (max-width: @screen-xl) {
        width: 100%;
        max-width: 480px;
        justify-content: center;
        padding-left: 0;
      }

      @media (max-width: @screen-md) {
        padding: 0 16px;
      }
    }

    // 登录表单包装器
    .login-form-wrapper {
      background: transparent;
      position: relative;
      z-index: 2;

      @media (max-width: @screen-xl) {
        width: 100%;
        max-width: 480px;
      }
    }

    // 移动端Logo样式
    .@{logo-prefix-cls} {
      position: absolute;
      top: 12px;
      height: 30px;
      z-index: 10;

      &__title {
        font-size: 16px;
        color: #fff;
      }

      img {
        width: 32px;
      }
    }

    // 第三方登录图标样式
    &-sign-in-way {
      .anticon {
        font-size: 22px;
        color: #888;
        cursor: pointer;
        transition: all 0.3s ease;

        &:hover {
          color: @primary-color;
          transform: scale(1.1);
        }
      }
    }

    // 输入框样式
    input:not([type='checkbox']) {
      min-width: 360px;

      @media (max-width: @screen-xl) {
        min-width: 320px;
      }

      @media (max-width: @screen-lg) {
        min-width: 260px;
      }

      @media (max-width: @screen-md) {
        min-width: 240px;
      }

      @media (max-width: @screen-sm) {
        min-width: 160px;
      }
    }

    .@{countdown-prefix-cls} input {
      min-width: unset;
    }

    .ant-divider-inner-text {
      font-size: 12px;
      color: @text-color-secondary;
    }

    &-form {
      background: transparent;
    }
  }

  // 页脚样式
  footer {
    position: relative;
    z-index: 10;

    .footer-text {
      color: rgba(255, 255, 255, 0.8);
      font-size: 14px;
      margin: 0;
      line-height: 1.5;
    }

    .footer-link {
      text-decoration: none;
      color: rgba(255, 255, 255, 0.9);
      position: relative;
      transition: all 0.3s ease;

      &:after {
        content: '';
        position: absolute;
        bottom: -2px;
        left: 0;
        width: 0;
        height: 1px;
        background: linear-gradient(135deg, #667eea, #764ba2);
        transition: width 0.3s ease;
      }

      &:hover {
        color: #fff;
        text-decoration: none;

        &:after {
          width: 100%;
        }
      }
    }

    @media (max-width: @screen-md) {
      padding: 16px 0;

      .container {
        padding: 0 12px;
      }

      .footer-text {
        font-size: 12px;
      }
    }
  }

  // 响应式优化
  @media (max-width: @screen-xl) {
    .@{prefix-cls} {
      .login-content {
        padding: 16px;
        padding-top: 60px;
      }

      .left-section {
        margin-bottom: 40px;
      }
    }
  }

  @media (max-width: @screen-lg) {
    .@{prefix-cls} {
      .login-content {
        padding: 12px;
        padding-top: 50px;
      }
    }
  }

  @media (max-width: @screen-md) {
    .@{prefix-cls} {
      .login-content {
        padding: 12px;
        padding-top: 50px;
      }

      .left-section {
        margin-bottom: 30px;
      }
    }
  }
</style>
